using System.ComponentModel.DataAnnotations;
using OneCaps.Core.Models;
using OneCaps.Core.Enum;

namespace OneCaps.Core.Entity
{
    /// <summary>
    /// 车辆聚合根
    /// </summary>
    public class Vehicle : BaseEntity<int>
    {
        /// <summary>
        /// 车牌号
        /// </summary>
        [Required]
        [MaxLength(20)]
        public string LicensePlate { get; set; } = string.Empty;

        /// <summary>
        /// 车辆类型
        /// </summary>
        public VehicleType Type { get; set; } = VehicleType.Private;

        /// <summary>
        /// 车辆品牌
        /// </summary>
        [MaxLength(50)]
        public string? Brand { get; set; }

        /// <summary>
        /// 车辆型号
        /// </summary>
        [MaxLength(50)]
        public string? Model { get; set; }

        /// <summary>
        /// 车辆颜色
        /// </summary>
        [MaxLength(20)]
        public string? Color { get; set; }

        /// <summary>
        /// 车主姓名
        /// </summary>
        [MaxLength(50)]
        public string? OwnerName { get; set; }

        /// <summary>
        /// 车主电话
        /// </summary>
        [MaxLength(20)]
        public string? OwnerPhone { get; set; }

        /// <summary>
        /// 是否VIP车辆
        /// </summary>
        public bool IsVip { get; set; } = false;

        /// <summary>
        /// 注册时间
        /// </summary>
        public DateTime RegisterTime { get; set; } = DateTime.Now;

        /// <summary>
        /// 最后入场时间
        /// </summary>
        public DateTime? LastEntryTime { get; set; }

        /// <summary>
        /// 当前停车记录
        /// </summary>
        public virtual ParkingRecord? CurrentParkingRecord { get; set; }

        /// <summary>
        /// 停车记录历史
        /// </summary>
        public virtual ICollection<ParkingRecord> ParkingRecords { get; set; } = new List<ParkingRecord>();

        // 领域方法
        /// <summary>
        /// 检查车辆是否正在停车
        /// </summary>
        public bool IsParking()
        {
            return CurrentParkingRecord != null && CurrentParkingRecord.Status == ParkingRecordStatus.Parking;
        }

        /// <summary>
        /// 车辆入场
        /// </summary>
        public void Enter(ParkingRecord record)
        {
            if (IsParking())
            {
                throw new InvalidOperationException($"车辆 {LicensePlate} 已在停车场内");
            }

            CurrentParkingRecord = record;
            LastEntryTime = DateTime.Now;
        }

        /// <summary>
        /// 车辆出场
        /// </summary>
        public void Exit()
        {
            if (!IsParking())
            {
                throw new InvalidOperationException($"车辆 {LicensePlate} 不在停车场内");
            }

            CurrentParkingRecord = null;
        }

        /// <summary>
        /// 验证车牌号格式
        /// </summary>
        public bool ValidateLicensePlate()
        {
            try
            {
                var licensePlate = new LicensePlate(LicensePlate);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
} 